package common.project.epic.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class Content {

	private EpicOpenHelper db ;
	private String table = "content";
	private SQLiteDatabase epicContent= db.getWritableDatabase();
	
	public Content(){}
	
	public int addContent(String id, String originmac, double lat, double longitude, int span, String life, int rank, String uri, String content, int idcat, int idtype, int hopcount, int repcount  ){
		
		String selection = "id";
		String[] selectionArgs = {id};
		Cursor result = epicContent.query(table, null, selection, selectionArgs, null, null, null);
		if(result.getCount()==0){
		ContentValues newContent = new ContentValues();
		newContent.put("id", id);
		newContent.put("originmac", originmac);
		newContent.put("lat", lat);
		newContent.put("long", longitude);
		newContent.put("span", span);
		newContent.put("life", life);
		newContent.put("rank", rank);
		newContent.put("uri", uri);
		newContent.put("content", content);
		newContent.put("id_category", idcat);
		newContent.put("id_type", idtype);
		newContent.put("hop_counter", hopcount);
		newContent.put("replication_counter", repcount);
		if (epicContent.insertOrThrow(table, null, newContent)!=-1){
			return 1; //riga inserita con successo
			}else{
			return -1; //errore nell'inserimento
			}
		}else{return 0;}//contenuto gi� presente
		
	}

	public Cursor getContents() {
		// TODO metodo che restituisce tutti i contenuti
		String groupBy = "category";
		Cursor result = epicContent.query(table, null, null, null, groupBy, null, null);
		if (result.getCount()==0){
		
			return null;
		
		}else{
			
			return result;
		}
	}
	
	public Cursor getContents(int category) {
		// TODO metodo che restituisce tutti i contenuti
		String selected_category = String.valueOf(category);
		String[] sel_cat_array = {selected_category};
		String groupBy = "category";
		Cursor result = epicContent.query(table, null, "category", sel_cat_array, groupBy, null, null);
		if (result.getCount()==0){
		
			return null;
		
		}else{
			
			return result;
		}
	}
	
	
	
}
